import pandas as pd
import matplotlib.pyplot as plt
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
#加载数据
df = pd.read_csv('NotClean_EVUsage_Data.csv')

# 将时间列转换为datetime类型
df['Start Date'] = pd.to_datetime(df['Start Date'])
df['End Date'] = pd.to_datetime(df['End Date'])
df['Transaction Date (Pacific Time)'] = pd.to_datetime(df['Transaction Date (Pacific Time)'])

# 将时长列转换为timedelta类型
df['Total Duration'] = pd.to_timedelta(df['Total Duration (hh:mm:ss)'])
df['Charging Time'] = pd.to_timedelta(df['Charging Time (hh:mm:ss)'])

# 绘制能量消耗的直方图
plt.figure(figsize=(10, 6))
plt.hist(df['Energy (kWh)'], bins=30, edgecolor='black')
plt.title('Energy Consumption Distribution')
plt.xlabel('Energy (kWh)')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()

# 绘制温室气体节省的直方图
plt.figure(figsize=(10, 6))
plt.hist(df['GHG Savings (kg)'], bins=30, edgecolor='black')
plt.title('GHG Savings Distribution')
plt.xlabel('GHG Savings (kg)')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()

# 绘制收费的直方图
plt.figure(figsize=(10, 6))
plt.hist(df['Fee'], bins=30, edgecolor='black')
plt.title('Fee Distribution')
plt.xlabel('Fee (USD)')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()

# 绘制充电时间的散点图
plt.figure(figsize=(10, 6))
plt.scatter(df['Start Date'], df['Charging Time'].dt.total_seconds() / 3600)
plt.title('Charging Time Over Time')
plt.xlabel('Start Date')
plt.ylabel('Charging Time (hours)')
plt.grid(True)
plt.show()

# 绘制能量消耗与收费的关系图
plt.figure(figsize=(10, 6))
plt.scatter(df['Energy (kWh)'], df['Fee'])
plt.title('Energy Consumption vs Fee')
plt.xlabel('Energy (kWh)')
plt.ylabel('Fee (USD)')
plt.grid(True)
plt.show()